﻿'------------------------------
' Hien thi 5 tin moi nhat
'------------------------------
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Namespace NVPortal.Modules.NVNews

    Public MustInherit Class SettingCustomeDisplaySpecial
        Inherits DotNetNuke.Entities.Modules.ModuleSettingsBase


#Region "Event Handlers"
        Public Overrides Sub LoadSettings()
            Try
                If (Page.IsPostBack = False) Then

                    'BindddlCategories()
                    Dim query As String = "select PortalId, PortalName from PortalLocalization"
                    BindDropDownList(ddlPortal, query, "PortalName", "PortalId", "Chọn trang")

                    ddlCategory.Enabled = True
                    ddlCategory.Items.Insert(0, New ListItem("Chọn danh mục", "0"))

                    ddlUrl.Enabled = True
                    ddlUrl.Items.Insert(0, New ListItem("Chọn URL", "0"))

                    If CType(ModuleSettings("PortalSetting"), String) <> "" Then
                        Me.ddlPortal.Items.FindByValue(ModuleSettings("PortalSetting")).Selected = True
                    End If
                    'Bind PortalAlias
                    Dim query2 As String = "select PortalAliasID, HTTPAlias from PortalAlias"
                    BindDropDownList(ddlUrl, query2, "HTTPAlias", "PortalAliasID", "Chọn Link")

                    If CType(ModuleSettings("NVNewsURLSetting"), String) <> "" Then
                        Me.ddlUrl.Items.FindByValue(ModuleSettings("NVNewsURLSetting")).Selected = True
                    End If
                    'Bind Danh muc
                    Dim ctlNewsCategories As New NV_NewsCategoriesController
                    Dim arrNewsCategories As New ArrayList
                    arrNewsCategories = ctlNewsCategories.GetAll(ModuleSettings("PortalSetting"))
                    Dim arrTemp As New ArrayList
                    Dim objNewsCategories As NV_NewsCategoriesInfo
                    Dim objNewsCategoriesTemp As NV_NewsCategoriesInfo

                    If arrNewsCategories.Count > 0 Then
                        For Each objNewsCategories In arrNewsCategories
                            If objNewsCategories.ParentId = 0 Then
                                arrTemp.Add(objNewsCategories)
                                For Each objNewsCategoriesTemp In arrNewsCategories
                                    If objNewsCategoriesTemp.ParentId = objNewsCategories.CategoryID Then
                                        objNewsCategoriesTemp.CategoryName = "--" & objNewsCategoriesTemp.CategoryName
                                        arrTemp.Add(objNewsCategoriesTemp)
                                    End If
                                Next
                            End If
                        Next
                    End If

                    Me.ddlCategory.DataSource = arrTemp
                    Me.ddlCategory.DataTextField = "CategoryName"
                    Me.ddlCategory.DataValueField = "CategoryId"
                    Me.ddlCategory.DataBind()
                    If CType(ModuleSettings("NVNewsCategorySetting"), String) <> "" Then
                        Me.ddlCategory.Items.FindByValue(ModuleSettings("NVNewsCategorySetting")).Selected = True
                    End If
                    'Het
                    Me.txtTitleNewsShow.Text = ModuleSettings("NVNewsTitlePageSetting")
                    If CType(ModuleSettings("NVNewsDisplayPageSetting"), String) <> "" Then
                        Me.txtDisplayNewsPage.Text = ModuleSettings("NVNewsDisplayPageSetting")
                    End If
                    If CType(ModuleSettings("NVNewsDisplayNumberSetting"), String) <> "" Then
                        Me.txtDisplayRow.Text = ModuleSettings("NVNewsDisplayNumberSetting")
                    End If
                    If CType(ModuleSettings("NVNewsDisplayStyleSetting"), String) <> "" Then
                        Me.ddlDisplayStyle.Items.FindByValue(ModuleSettings("NVNewsDisplayStyleSetting")).Selected = True
                    End If
                    If CType(ModuleSettings("NVNewsImageValueSetting"), String) <> "" Then
                        Me.txtImageWidth.Text = Split(ModuleSettings("NVNewsImageValueSetting"))(0)
                        Me.txtImageHeight.Text = Split(ModuleSettings("NVNewsImageValueSetting"))(1)
                    End If

                    
                End If

            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' UpdateSettings saves the modified settings to the Database
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''		[cnurse]	10/22/2004	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Overrides Sub UpdateSettings()
            Try
                Dim objModules As New DotNetNuke.Entities.Modules.ModuleController
                objModules.UpdateModuleSetting(ModuleId, "PortalSetting", Me.ddlPortal.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "NVNewsURLSetting", Me.ddlUrl.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "NVNewsCategorySetting", Me.ddlCategory.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "NVNewsTitlePageSetting", Me.txtTitleNewsShow.Text)
                If IsNumeric(Me.txtDisplayNewsPage.Text) Then
                    objModules.UpdateModuleSetting(ModuleId, "NVNewsDisplayPageSetting", Me.txtDisplayNewsPage.Text)
                End If
                objModules.UpdateModuleSetting(ModuleId, "NVNewsDisplayNumberSetting", IIf(Trim(Me.txtDisplayRow.Text) <> "", Trim(Me.txtDisplayRow.Text), "1"))
                objModules.UpdateModuleSetting(ModuleId, "NVNewsDisplayStyleSetting", Me.ddlDisplayStyle.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "NVNewsImageValueSetting", IIf(Trim(Me.txtImageWidth.Text) <> "", Trim(Me.txtImageWidth.Text), "0") & " " & IIf(Trim(Me.txtImageHeight.Text) <> "", Trim(Me.txtImageHeight.Text), "0"))


            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        'Private Sub BindddlCategories()
        '    Dim ctlNewsCategories As New NV_NewsCategoriesController
        '    Dim arrNewsCategories As New ArrayList
        '    arrNewsCategories = ctlNewsCategories.GetAll(PortalId)
        '    Dim arrTemp As New ArrayList
        '    Dim objNewsCategories As NV_NewsCategoriesInfo
        '    Dim objNewsCategoriesTemp As NV_NewsCategoriesInfo

        '    If arrNewsCategories.Count > 0 Then
        '        For Each objNewsCategories In arrNewsCategories
        '            If objNewsCategories.ParentId = 0 Then
        '                arrTemp.Add(objNewsCategories)
        '                For Each objNewsCategoriesTemp In arrNewsCategories
        '                    If objNewsCategoriesTemp.ParentId = objNewsCategories.CategoryID Then
        '                        objNewsCategoriesTemp.CategoryName = "--" & objNewsCategoriesTemp.CategoryName
        '                        arrTemp.Add(objNewsCategoriesTemp)
        '                    End If
        '                Next
        '            End If
        '        Next
        '    End If

        '    Me.ddlCategory.DataSource = arrTemp
        '    Me.ddlCategory.DataTextField = "CategoryName"
        '    Me.ddlCategory.DataValueField = "CategoryId"
        '    Me.ddlCategory.DataBind()

        'End Sub
        Private Sub BindDropDownList(ByVal ddl As DropDownList, ByVal query As String, ByVal text As String, ByVal value As String, ByVal defaultText As String)
            Dim conString As String = ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString
            Dim cmd As New SqlCommand(Query)
            Using con As New SqlConnection(conString)
                Using sda As New SqlDataAdapter()
                    cmd.Connection = con
                    con.Open()
                    ddl.DataSource = cmd.ExecuteReader()
                    ddl.DataTextField = text
                    ddl.DataValueField = value
                    ddl.DataBind()
                    con.Close()
                End Using
            End Using
            ddl.Items.Insert(0, New ListItem(defaultText, "0"))
        End Sub
        Protected Sub Country_Changed(ByVal sender As Object, ByVal e As EventArgs)
            ddlCategory.Enabled = False
            ddlCategory.Items.Clear()
            ddlCategory.Items.Insert(0, New ListItem("Select State", "0"))

            ddlUrl.Enabled = False
            ddlUrl.Items.Clear()
            ddlUrl.Items.Insert(0, New ListItem("Select kec", "0"))

            Dim countryId As Integer = Integer.Parse(ddlPortal.SelectedItem.Value)
            If countryId > 0 Then
                Dim query As String = String.Format("select CategoryID, CategoryName from NV_NewsCategories where PortalId = {0}", countryId)
                BindDropDownList(ddlCategory, query, "CategoryName", "Categoryid", "Chọn danh mục")
                ddlCategory.Enabled = True

                Dim query2 As String = String.Format("select PortalAliasID, HTTPAlias from PortalAlias where PortalId = {0}", countryId)
                BindDropDownList(ddlUrl, query2, "HTTPAlias", "PortalAliasID", "Chọn Link")
                ddlUrl.Enabled = True
            End If
        End Sub
        
#End Region


    End Class

End Namespace
